package edu.washington.ling.syntric.proc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.washington.ling.syntric.struct.tree.Token;
import edu.washington.ling.syntric.struct.tree.Tree;

public class CompileDataStatistics {
	private static Map<String, Integer> featTypeMap = new HashMap<String, Integer>();
	private static Map<String, Integer> depTypeMap = new HashMap<String, Integer>();
	
	private static void collectFeatTypes(Tree tree) {
		for (Token t: tree.getTokenList()) {
			for (String featType: t.getFeatures()) {
				if (featTypeMap.containsKey(featType)) {
					int count = featTypeMap.get(featType) + 1;
					featTypeMap.put(featType, count);
				} else {
					featTypeMap.put(featType, 1);
				}
			}
		}

	}
	private static void collectDepTypes(Tree tree) {
		for (Token t: tree.getTokenList()) {
			if (t.hasDSHead()) {
				String relType = t.getDSRelation();
				if (depTypeMap.containsKey(relType)) {
					int count = depTypeMap.get(relType) + 1;
					depTypeMap.put(relType, count);
				} else {
					depTypeMap.put(relType, 1);
				}
			}
		}
	}
	private static void printTypes(Map<String, Integer> typeMap) {
		List<Map.Entry<String, Integer>> entryList 
			= new ArrayList<Map.Entry<String, Integer>>(typeMap.entrySet());
		Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
			public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
				return o2.getValue() - o1.getValue();
			}
		});
		for (Map.Entry<String, Integer> entry: entryList) {
			String output = String.format("%s\t%d", entry.getKey(), entry.getValue());
			System.out.println(output);
		}
	}
}
